package me.guyongquan.cal10npropwriter.popup.actions;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import me.guyongquan.cal10npropwriter.EnumResourceVisitor;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class WriteToPop implements IObjectActionDelegate {

	private Shell shell;
	private IStructuredSelection structuredSelection;
	
	/**
	 * Constructor for Action1.
	 */
	public WriteToPop() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		ICompilationUnit cu = (ICompilationUnit) structuredSelection .getFirstElement();
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setSource(cu);
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		File sourceFile = cu.getResource().getLocation().toFile();
		EnumResourceVisitor visitor = new EnumResourceVisitor();
		parser.createAST(null).accept(visitor);
		File resourceDir = sourceFile;
		while(resourceDir != null && resourceDir.getParentFile() != null) {
			if(resourceDir.getName().equals("main") && resourceDir.getParentFile().getName().equals("src")) {
				resourceDir = new File(resourceDir, "resources");
				break;
			}
			resourceDir = resourceDir.getParentFile();
		}
		if(resourceDir == null) {
			return;
		}
		if(visitor.write(resourceDir)) {
			try {
				String baseName = visitor.getBaseName();
				String path = baseName.replace('.', '/');
				int lastIndex = path.lastIndexOf('/');
				path = lastIndex == -1 ? "" : '/' + path.substring(0, path.lastIndexOf('/'));
				cu.getResource().getProject().getFolder("/src/main/resources" + path).refreshLocal(org.eclipse.core.resources.IResource.DEPTH_ONE, null);
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private String readFile(File sourceFile) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream(
					(int) sourceFile.length());
			FileInputStream is = new FileInputStream(sourceFile);
			try {
				int data;
				while ((data = is.read()) != -1) {
					baos.write(data);
				}
			} finally {
				is.close();
			}
			return new String(baos.toByteArray(), "utf-8");
		} catch (Exception e) {
			throw new IllegalStateException(e);
		}
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		if(!(selection instanceof IStructuredSelection)) {
			return;
		}
		structuredSelection = (org.eclipse.jface.viewers.IStructuredSelection)selection;
	}

}
